<?php
/**
 * Created by Zend Studio.
 * User: FDG348
 * Date: 2017/11/06
 * Time: 11:36
 */
namespace common\models;

use dosamigos\qrcode\lib\Tools;
use yii;
use yii\db\ActiveRecord;

class CouponReceive extends ActiveRecord{
    const STATUS_ENABLE = 1;			//是否使用
    const STATUS_DISABLE = 0;

    const IS_EXCHANGE_ENABLE = 1;		//是否兑换
    const IS_EXCHANGE_DISABLE = 0;
    
    public static function tableName(){
        return '{{%coupon_receive}}';
    }

    public function rules(){
        return [
            ['use_flg','default','value'=>self::STATUS_DISABLE],
            ['is_exchange','default','value'=>self::IS_EXCHANGE_DISABLE]
        ];
    }

    /**
     * @获取创建人
     */
    public function getCreator(){
        $userInfo = \common\helps\tools::getloginUserInfo();
        if($userInfo){
            $userName = $userInfo->userName;
        }else{
            $userName = '';
        }
        return $userName;
    }

	/**
	 * @param 获取未兑换未过期的领取优惠券信息
	 * @return array|\yii\db\ActiveRecord[]
	 */
	public static function getNoExchange(){
		$time = date('Y-m-d');
		$sql = "SELECT `j_cust_master`.`wx_openid`, `j_coupon_receive`.`content`, `j_coupon_receive`.`mobile`,`j_coupon_receive`.`sms_verification`,`j_coupon_receive`.`coupon_end_time`
                FROM `j_coupon_receive` LEFT JOIN `j_cust_master` ON `j_coupon_receive`.`cust_master_id` = `j_cust_master`.`id`
                WHERE (`j_coupon_receive`.`use_flg`=0) AND (`j_coupon_receive`.`is_exchange`=0) AND
                ('".$time."' BETWEEN `j_coupon_receive`.`coupon_start_time` AND `j_coupon_receive`.`coupon_end_time`) AND
                `j_cust_master`.`wx_openid` IS NOT NULL";
		return \common\helps\tools::getDriverDb()->createCommand($sql)->queryAll();
	}

	/**
	 * @param 获取未兑换未过期的领取优惠券信息
	 * @return array|\yii\db\ActiveRecord[]
	 */
	public static function getSmsNoExchange(){
		$time = date('Y-m-d');
		$sql = "SELECT COUNT(1) AS num, `j_cust_master`.`wx_openid`, `j_coupon_receive`.`mobile` FROM `j_coupon_receive` LEFT JOIN `j_cust_master` ON `j_coupon_receive`.`cust_master_id` = `j_cust_master`.`id` WHERE (`j_coupon_receive`.`use_flg`=0) AND (`j_coupon_receive`.`coupon_start_time` < '".$time."') AND (`j_coupon_receive`.`coupon_end_time` > '".$time."') GROUP BY `j_coupon_receive`.`cust_master_id`";
		return \common\helps\tools::getDriverDb()->createCommand($sql)->queryAll();
	}
}